package com.tanhua.dubbo.api;

import com.tan.model.mongo.UserLike;
import com.tan.model.vo.CountVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@DubboService
public class LikeUserapiimpl implements LikeUserapi {
    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public void likeUser(Long userId, Long likeUserId) {

            Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
            Query query = Query.query(criteria);
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if(userLike == null){
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setIsLike(true);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);
            }else {
                userLike.setIsLike(true);
                userLike.setUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLike);

            }

        }

    @Override
    public Boolean likeUsereacher(Long likeUserId, Long userId) {
        Criteria criteria = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        if(mongoTemplate.exists(Query.query(criteria),UserLike.class)){
            return true;
        }
        return false;
    }

    @Override
    public void unlikeUser(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Query query = Query.query(criteria);
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        if(userLike == null){
            userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setIsLike(false);
            userLike.setCreated(System.currentTimeMillis());
            userLike.setUpdated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }else {
            userLike.setIsLike(false);
            userLike.setUpdated(System.currentTimeMillis());
            mongoTemplate.save(userLike);

        }
    }

    @Override
    public CountVo getcounts(Long userId, Integer likecount, Set<Long> set) {
        Criteria criteria = Criteria.where("likeUserId").is(userId).and("isLike").is(true);
        Query query = Query.query(criteria);
      Integer fanscount = Math.toIntExact(mongoTemplate.count(query, UserLike.class));
      Integer eachercount=0;
      for (Long o : set) {
          Criteria criteria1 = Criteria.where("userId").is(o).and("isLike").is(true).and("likeUserId").is(userId);
          Query query1 = Query.query(criteria1);
          if (mongoTemplate.exists(query1, UserLike.class)){
              eachercount++;
          }
      }
      CountVo countVo = new CountVo();
      countVo.setFanCount(fanscount);
      countVo.setEachLoveCount(eachercount);
      countVo.setLoveCount(likecount);

        return countVo;
    }

}

